// Copyright (C) 2009 Jesse Jones
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using MmmDoc;
using MmmDoc.Database;
using System;
using System.IO;

namespace MmmDoc.Html
{
	/// <summary>This namespace contains the code used to generate docs using the HTML format.</summary>
	internal sealed class NamespaceDoc
	{
	}
	
	/// <summary>Top level class used when generating HTML documentation.</summary>
	internal sealed class Generate
	{
		public Generate(Document doc)
		{
			m_doc = doc;
		}
		
		/// <summary>This is what actually generates the documentation.</summary>
		public void Write()
		{
			// Generate the namespaces file.
			if (Settings.Verbosity >= 2)
				Console.WriteLine("generating namespaces files");
			
			if (!Directory.Exists(Settings.OutDir))
				Directory.CreateDirectory(Settings.OutDir);
			
			var n1 = new GenerateNamespacesFrameset();
			n1.Write(Settings.OutDir);
			
			var n3 = new GenerateNamespacesFile(m_doc);
			n3.Write(Settings.OutDir);
			
			var n2 = new GenerateNamespacesToc(n3.Namespaces);
			n2.Write(Settings.OutDir);
			
			// Generate each type file.
			string dir = Path.Combine(Settings.OutDir, "types");
			if (Directory.Exists(dir))
				Directory.Delete(dir, true);
			Directory.CreateDirectory(dir);
			
			foreach (TypeDef type in m_doc.Types)
			{
				if (!type.IsExcluded())
				{
					if (Settings.Verbosity >= 2)
						Console.WriteLine("generating {0} files", type.FullName);
						
					if (Settings.WarnIfMissing && type.Definition == null)
						Console.Error.WriteLine("{0} is missing documentation.", type.FullName);
					
					var t1 = new GenerateTypeFrameset(type);
					t1.Write(dir);
					
					var t2 = new GenerateTypeFile(type);
					t2.Write(dir);
					
					var t3 = new GenerateTypeToc(type);
					t3.Write(dir);
				}
			}
		}
		
		#region Fields
		private Document m_doc;
		#endregion
	}
}
